home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue50 / IPC / Named Pipes / Delphi / ChildMainFormUnit.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-28  |  1.2 KB  |  58 lines

  1. unit ChildMainFormUnit;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Edit1: TEdit;
  12.     Button1: TButton;
  13.     procedure FormCreate(Sender: TObject);
  14.     procedure FormDestroy(Sender: TObject);
  15.     procedure Button1Click(Sender: TObject);
  16.   private
  17.     PipeWrite: THandle;
  18.   end;
  19.  
  20. {$ifdef Ver90}
  21.   //This exception class did not exist in Delphi 2
  22.   EWin32Error = class(Exception);
  23. {$endif}
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. const
  29.   PipeNameFixedPrefix = '\\.\pipe\';
  30.   PipeName = PipeNameFixedPrefix + 'SampleNamedPipe';
  31.  
  32. implementation
  33.  
  34. {$R *.DFM}
  35.  
  36. procedure TForm1.FormCreate(Sender: TObject);
  37. begin
  38.   PipeWrite := FileOpen(PipeName, fmOpenWrite);
  39.   if PipeWrite = Invalid_Handle_Value then
  40.     raise EWin32Error.Create('Cannot open pipe for writing');
  41. end;
  42.  
  43. procedure TForm1.FormDestroy(Sender: TObject);
  44. begin
  45.   CloseHandle(PipeWrite);
  46. end;
  47.  
  48. procedure TForm1.Button1Click(Sender: TObject);
  49. var
  50.   Msg: String;
  51. begin
  52.   Msg := Edit1.Text + #13#10;
  53.   if FileWrite(PipeWrite, Msg[1], Length(Msg)) = Integer(HFile_Error) then
  54.     raise EWin32Error.Create('Cannot write to named pipe');
  55. end;
  56.  
  57. end.
  58.